package com.yjd.comm.base.tag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;

import com.yjd.comm.util.FrameUtil;
import com.yjd.comm.util.StringUtil;

public class HtmlTitleTag extends BodyTagSupport {

	/**
	 * @Fields serialVersionUID
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public int doEndTag() throws JspException {
		String defaultTitle = FrameUtil.getWebResource("title");
		String requestTitle = this.pageContext.getRequest().getAttribute(
				"HEAD_TITLE") != null ? this.pageContext.getRequest()
				.getAttribute("HEAD_TITLE").toString() : null;
//		System.out.println(this.pageContext.getRequest().getAttribute(
//				"HEAD_TITLE") == null);
		String tilesTitle = this.getBodyContent() != null ? this
				.getBodyContent().getString() : null;
		String title = null;
		try {
			if (StringUtil.isNotEmpty(requestTitle)) {
				title = requestTitle;
			} else if (StringUtil.isNotEmpty(tilesTitle)) {
				title = tilesTitle;
			} else {
				title = defaultTitle;
			}
			this.pageContext.getOut().print(title);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		return super.doEndTag();
	}

}
